
#include "AudioManager.h"
#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioServices.h>

@implementation AudioManager
static AudioManager* _instance = nil;

+(AudioManager*)instance
{
	@synchronized([AudioManager class])
	{
		if (!_instance)
			[[self alloc] init];
        
		return _instance;
	}
    
	return nil;
}

+(id)alloc
{
	@synchronized([AudioManager class])
	{
		NSAssert(_instance == nil, @"Attempted to allocate a second instance of a singleton.");
		_instance = [super alloc];
		return _instance;
	}
    
	return nil;
}

-(id)init {
	self = [super init];
	if (self != nil) 
    {
        m_dMusic = [[NSMutableDictionary alloc] init];	
        m_dSfx = [[NSMutableDictionary alloc] init];	
    }
    
	return self;
}

-(void)preloadMusic:(const char*) _file
{
    NSString *file = [NSString stringWithUTF8String:_file];
    if([m_dMusic objectForKey:file] == nil)
    {
        NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
        NSString *fullPath = [resourceDir stringByAppendingPathComponent:file];
        NSURL *url = [NSURL fileURLWithPath:fullPath];
        AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [m_dMusic setObject:click forKey:file];
    }
}

-(void)preloadSfx:(const char*) _file
{
    NSString *file = [NSString stringWithUTF8String:_file];
    if([m_dSfx objectForKey:file] == nil)
    {
        NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
        NSString *fullPath = [resourceDir stringByAppendingPathComponent:file];
        NSURL *filePath = [NSURL fileURLWithPath:fullPath isDirectory:NO];
        
        SystemSoundID soundID;
        AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
        [m_dSfx setObject:[NSNumber numberWithUnsignedLong:soundID] forKey:file];
    }
}

-(void)unloadMusic:(const char*) _file
{
    NSString *file = [NSString stringWithUTF8String:_file];
    if([m_dMusic objectForKey:file] != nil)
    {
        AVAudioPlayer* click = [m_dMusic objectForKey:file];
        [m_dMusic removeObjectForKey:file];
        [click dealloc];
    }
}

-(void)unloadSfx:(const char*) _file
{
    NSString *file = [NSString stringWithUTF8String:_file];
    if([m_dSfx objectForKey:file] != nil)
    {
        NSNumber* num = [m_dSfx objectForKey:file];
        SystemSoundID soundID = [num unsignedLongValue];
        AudioServicesDisposeSystemSoundID(soundID);
    }
}

-(void)playMusic:(const char*) _file
{
    AVAudioPlayer *click;
    NSString *file = [NSString stringWithUTF8String:_file];
    if([m_dMusic objectForKey:file] != nil)
    {
        click = [m_dMusic objectForKey:file];
    }
    else
    {
        NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
        NSString *fullPath = [resourceDir stringByAppendingPathComponent:file];
        NSURL *url = [NSURL fileURLWithPath:fullPath];
        click = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [m_dMusic setObject:click forKey:file];
    }
    
    click.currentTime = 0;
    [click play];
}

-(void)playSfx:(const char*) _file
{
    SystemSoundID soundID;
    NSString *file = [NSString stringWithUTF8String:_file];
    if([m_dSfx objectForKey:file] != nil)
    {
        NSNumber* num = [m_dSfx objectForKey:file];
        soundID = [num unsignedLongValue];
    }
    else
    {
        NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
        NSString *fullPath = [resourceDir stringByAppendingPathComponent:file];
        NSURL *filePath = [NSURL fileURLWithPath:fullPath isDirectory:NO];
        AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
        [m_dSfx setObject:[NSNumber numberWithUnsignedLong:soundID] forKey:file];
    }
    
    //Use audio services to play the sound
    AudioServicesPlaySystemSound(soundID);
    
}

-(void)setMusicVolume:(const char *)_file volume:(float)_volume
{
    NSString *file = [NSString stringWithUTF8String:_file];
    if([m_dMusic objectForKey:file] != nil)
    {
        AVAudioPlayer *click = [m_dMusic objectForKey:file];
        click.volume = _volume;
    }
}

-(void)setMusicLoop:(const char *)_file loop:(bool)_loop
{
    NSString *file = [NSString stringWithUTF8String:_file];
    if([m_dMusic objectForKey:file] != nil)
    {
        AVAudioPlayer *click = [m_dMusic objectForKey:file];
        click.numberOfLoops = _loop ? -1 : 0;
    }
}

-(void)stopMusic:(const char *)_file
{
    NSString *file = [NSString stringWithUTF8String:_file];
    if([m_dMusic objectForKey:file] != nil)
    {
        AVAudioPlayer *click = [m_dMusic objectForKey:file];
        [click stop];
    }
}

-(void)setGeneralMusicVolume:(float) _volume
{
    for (AVAudioPlayer* audio in m_dMusic.objectEnumerator)
    {
        audio.volume = _volume;
    }
}

@end
